package com.rtssoft.scalatest

import java.text.SimpleDateFormat
import java.util.Date

// we need something like that: http://www.javapractices.com/topic/TopicAction.do?Id=13
// something that can handle math operations with fixed number after point
// another question - where to place number of these points
trait TimeValue {
    val time: Date
}

trait Replaceable {
   
    def replace[T <: TimeValue](v: T) = (this.asInstanceOf[AnyRef], v) match {
        case (thisObj @ QuoteTick(_,_,_), y @ QuoteTick(_,_,_)) => { 
                thisObj.ask = y.ask; thisObj.bid = y.bid;
            }
            
        case (x,y) =>  println("Unknown replacement: " + x + " to " + y)
    }
}


object QuoteTick {
    val fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
}


case class QuoteTick(val time: Date, 
                     var bid: BigDecimal, 
                     var ask: BigDecimal) extends TimeValue with Replaceable {
    
    def this(tm: Long, b: Double, a: Double) = this(new Date(tm), b, a)
    
    
    override def toString() = format("<q %s : %f %f>", QuoteTick.fmt.format(time), bid, ask)
}


object TStest {

    implicit def tuple2quote1(x: Tuple3[Date,Double,Double]) = new QuoteTick(x._1, x._2, x._3)

    implicit def tuple2quote2(x: Tuple3[Long,Double,Double]) = new QuoteTick(x._1, x._2, x._3)
    
    implicit def tuple2quote3(x: Tuple3[String,Double,Double]) = new QuoteTick(QuoteTick.fmt.parse(x._1), x._2, x._3)
    
        
    def bigDecimalsInvestigations(): Unit = {
        var x1: BigDecimal = 20.00001
        var x2: BigDecimal = 30.14150
        var x3: BigDecimal = 30.00

        println(" -- Big decimals --")
                
        for(x <- 1 to 10) {
            x1 += 0.00001
            println("x1 = " + x1)
        }
               
        println("x1 = " + x1)
        println("res1 = " + (x2 - x1))
        println("res2 = " + (x3 - 0.00001))
    }


    def tsTest(): Unit = {
        println("TimeSeries test...")

        val lq = List[QuoteTick](("2010-11-22 10:10:00", 1.0200, 1.0210),
                                 ("2010-11-22 10:20:00", 2.0000,2.0001),
                                 ("2010-11-22 10:30:00", 3.0000, 3.1000))
        
        lq(1).replace[QuoteTick]("2010-11-22 10:20:00", 555.0000, 555.0001)
        lq(2).replace[TimeValue](new QuoteTick(QuoteTick.fmt.parse("2010-11-22 10:30:00"), 111, 222))

        lq.foreach(x => println(x.toString + " " + x.ask + " / " + x.bid))
    }
}